<div class="container">
    <h1 id="pkg-overview">package parser</h1>
    <p><code>import "go/parser"</code>
    </p><p>Package parser implements a parser for Go source files. Input may be provided in a variety of forms (see the various Parse* functions); the output is an abstract syntax tree (AST) representing the Go source. The parser is invoked through one of the Parse* functions.</p>
    
    
		
        
        
        
        
    
    
    <h2 id="ParseDir">func ParseDir </h2>
    <pre>func ParseDir(fset *<a href="./go_token.htm">token</a>.<a href="./go_token.htm#FileSet">FileSet</a>, path <a href="./builtin.htm#string">string</a>, filter func(<a href="./os.htm">os</a>.<a href="./os.htm#FileInfo">FileInfo</a>) <a href="./builtin.htm#bool">bool</a>, mode <a href="#Mode">Mode</a>) (pkgs map[<a href="./builtin.htm#string">string</a>]*<a href="./go_ast.htm">ast</a>.<a href="./go_ast.htm#Package">Package</a>, first <a href="./builtin.htm#error">error</a>)</pre>
            <p>
ParseDir calls ParseFile for all files with names ending in ".go" in the
directory specified by path and returns a map of package name -&gt; package
AST with all the packages found.
</p>
<p>
If filter != nil, only the files with os.FileInfo entries passing through
the filter (and ending in ".go") are considered. The mode bits are passed
to ParseFile unchanged. Position information is recorded in fset.
</p>
<p>
If the directory couldn't be read, a nil map and the respective error are
returned. If a parse error occurred, a non-nil but incomplete map and the
first error encountered are returned.
</p>


    <h2 id="ParseExpr">func ParseExpr </h2>
    <pre>func ParseExpr(x <a href="./builtin.htm#string">string</a>) (<a href="./go_ast.htm">ast</a>.<a href="./go_ast.htm#Expr">Expr</a>, <a href="./builtin.htm#error">error</a>)</pre>
            <p>
ParseExpr is a convenience function for obtaining the AST of an expression x.
The position information recorded in the AST is undefined. The filename used
in error messages is the empty string.
</p>

    <h2 id="ParseFile">func ParseFile </h2>
    <pre>func ParseFile(fset *<a href="./go_token.htm">token</a>.<a href="./go_token.htm#FileSet">FileSet</a>, filename <a href="./builtin.htm#string">string</a>, src interface{}, mode <a href="#Mode">Mode</a>) (f *<a href="./go_ast.htm">ast</a>.<a href="./go_ast.htm#File">File</a>, err <a href="./builtin.htm#error">error</a>)</pre>
            <p>
ParseFile parses the source code of a single Go source file and returns
the corresponding ast.File node. The source code may be provided via
the filename of the source file, or via the src parameter.
</p>
<p>
If src != nil, ParseFile parses the source from src and the filename is
only used when recording position information. The type of the argument
for the src parameter must be string, []byte, or io.Reader.
If src == nil, ParseFile parses the file specified by filename.
</p>
<p>
The mode parameter controls the amount of source text parsed and other
optional parser functionality. Position information is recorded in the
file set fset.
</p>
<p>
If the source couldn't be read, the returned AST is nil and the error
indicates the specific failure. If the source was read but syntax
errors were found, the result is a partial AST (with ast.Bad* nodes
representing the fragments of erroneous source code). Multiple errors
are returned via a scanner.ErrorList which is sorted by file position.
</p>

            <div class="toggle" id="example_ParseFile">

    <div class="expanded">
        
        
        
            <p>Code:</p>
            <pre class="code">fset := token.NewFileSet() <span class="comment">// positions are relative to fset</span>

<span class="comment">// Parse the file containing this very example</span>
<span class="comment">// but stop after processing the imports.</span>
f, err := parser.ParseFile(fset, "example_test.go", nil, parser.ImportsOnly)
if err != nil {
    fmt.Println(err)
    return
}

<span class="comment">// Print the imports from the file's AST.</span>
for _, s := range f.Imports {
    fmt.Println(s.Path.Value)
}

<span class="comment"></span></pre>
            
            <p>Output:</p>
            <pre class="output">"fmt"
"go/parser"
"go/token"
</pre>
            
        
    </div>
</div>

<h2 id="Mode">type Mode </h2>
<pre>type Mode <a href="./builtin.htm#uint">uint</a></pre>
            <p>
A Mode value is a set of flags (or 0).
They control the amount of source code parsed and other optional
parser functionality.
</p>


            
                <pre>const (
    <span id="PackageClauseOnly">PackageClauseOnly</span> <a href="#Mode">Mode</a>             = 1 &lt;&lt; <a href="./builtin.htm#iota">iota</a> <span class="comment">// stop parsing after package clause</span>
    <span id="ImportsOnly">ImportsOnly</span>                                    <span class="comment">// stop parsing after import declarations</span>
    <span id="ParseComments">ParseComments</span>                                  <span class="comment">// parse comments and add them to AST</span>
    <span id="Trace">Trace</span>                                          <span class="comment">// print a trace of parsed productions</span>
    <span id="DeclarationErrors">DeclarationErrors</span>                              <span class="comment">// report declaration errors</span>
    <span id="SpuriousErrors">SpuriousErrors</span>                                 <span class="comment">// same as AllErrors, for backward-compatibility</span>
    <span id="AllErrors">AllErrors</span>         = <a href="#SpuriousErrors">SpuriousErrors</a>             <span class="comment">// report all errors (not just the first 10 on different lines)</span>
)</pre>

</div>